package org.opens.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class EntityRandomUtil {

    /**
     * 通过反射批量获取模拟对象
     * @param classInfo 反射要获取的类的class信息
     * @param num 获取的个数
     */
    public static <T> List<T> getList(Class<T> classInfo, int num) throws InstantiationException, IllegalAccessException {
        List<T> result = new ArrayList<>();

        Field[] fields = classInfo.getDeclaredFields();
        Random random = new Random();

        for (int i = 0; i < num; i++) {
            T instance = classInfo.newInstance();
            for (Field field : fields) {
                String typeName = field.getGenericType().getTypeName();
                String fieldName = field.getName();

                field.setAccessible(true);

                if ("java.lang.String".equals(typeName)) {
                    field.set(instance, fieldName + i);
                }

                if ("java.lang.Integer".equals(typeName)) {
                    field.set(instance, random.nextInt());
                }

                if ("java.util.Date".equals(typeName)) {
                    field.set(instance, new Date());
                }
            }
            result.add(instance);
        }

        return result;
    }

}
